package com.dieusoft.box.companydata.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dieusoft.box.companydata.entity.CompanyData;
import com.dieusoft.box.companydata.service.ICompanyDataService;
import com.dieusoft.box.feedata.entity.FeeNameData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 公司信息数据库
 * @Author: jeecg-boot
 * @Date: 2021-03-22
 * @Version: V1.0
 */
@Api(tags = "公司信息数据库")
@RestController
@RequestMapping("/companydata")
@Slf4j
public class CompanyDataController extends JeecgController<CompanyData, ICompanyDataService> {
    @Autowired
    private ICompanyDataService companyDataService;

    /**
     * 分页列表查询
     *
     * @param companyData
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "公司信息数据库-分页列表查询")
    @ApiOperation(value = "公司信息数据库-分页列表查询", notes = "公司信息数据库-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CompanyData companyData,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CompanyData> queryWrapper = QueryGenerator.initQueryWrapper(companyData, req.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //状态100代表审核通过的
        queryWrapper.eq("status", 100);
        queryWrapper.eq("sys_org_code", sysUser.getOrgCode());
        Page<CompanyData> page = new Page<CompanyData>(pageNo, pageSize);
        IPage<CompanyData> pageList = companyDataService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 未提交
     *
     * @param companyData
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "公司信息数据库-分页列表查询", notes = "公司信息数据库-分页列表查询")
    @GetMapping(value = "/nosubmit")
    public Result<?> queryNoSubmit(CompanyData companyData,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CompanyData> queryWrapper = QueryGenerator.initQueryWrapper(companyData, req.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //查询自己未提交的数据和驳回的数据
        queryWrapper.in("status", 1, 3);
        queryWrapper.eq("create_by", sysUser.getUsername());
        Page<CompanyData> page = new Page<CompanyData>(pageNo, pageSize);
        IPage<CompanyData> pageList = companyDataService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @ApiOperation(value = "提交审核", notes = "提交审核")
    @PostMapping(value = "/submit")
    public Result<?> submit(@RequestBody JSONObject ids) {
        List<Object> ids1 = ids.getJSONArray("ids").stream().collect(Collectors.toList());
        UpdateWrapper<CompanyData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 2);
        updateWrapper.in("id", ids1);
        boolean update = companyDataService.update(updateWrapper);
        return update ? Result.OK("已提交审核!") : Result.error("提交失败");
    }

    /**
     * 审核中
     *
     * @param companyData
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "公司信息数据库-分页列表查询", notes = "公司信息数据库-分页列表查询")
    @GetMapping(value = "/progress")
    public Result<?> queryProgress(CompanyData companyData, Integer type,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CompanyData> queryWrapper = QueryGenerator.initQueryWrapper(companyData, req.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //查询审核中的数据
        queryWrapper.eq("status", 2);
        //如果type==0就查询自己提交待审核的数据
        if (null == type) {
            queryWrapper.eq("create_by", sysUser.getUsername());
        } else {
            queryWrapper.eq("sys_org_code", sysUser.getOrgCode());
        }
        Page<CompanyData> page = new Page<CompanyData>(pageNo, pageSize);
        IPage<CompanyData> pageList = companyDataService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 驳回
     *
     * @param jsonData
     * @return
     */
    @AutoLog(value = "驳回")
    @PostMapping(value = "/reject")
    public Result<?> reject(@RequestBody JSONObject jsonData) {
        String userName = jsonData.getString("userName");
        String id = jsonData.getString("id");
        if (!StringUtils.isAnyBlank(userName, id)) {
            CompanyData companyData = companyDataService.getById(id);
            companyData.setCreateBy(userName);
            companyData.setStatus("3");
            companyDataService.updateById(companyData);
            return Result.OK().success("已驳回到" + userName + "的账户下");
        } else {
            return Result.error("请选择数据");
        }
    }

    /**
     * 审核通过
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/approved")
    public Result<?> approved(@RequestBody JSONObject ids) {
        List<Object> ids1 = ids.getJSONArray("ids").stream().collect(Collectors.toList());
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("status", 100);
        updateWrapper.in("id", ids1);
        boolean update = companyDataService.update(updateWrapper);
        return update ? Result.OK("审核通过!") : Result.error("审核失败");
    }

    /**
     * 撤销审核
     *
     * @param jsonData
     * @return
     */
    @PostMapping(value = "/revoke")
    public Result<?> revoke(@RequestBody JSONObject jsonData) {
        String id = jsonData.getString("id");
        if (StringUtils.isNoneBlank(id)) {
            CompanyData companyData = companyDataService.getById(id);
            companyData.setStatus("2");
            companyDataService.updateById(companyData);
            return Result.OK().success("撤销完成!");
        } else {
            return Result.error("请选择要撤销审核的数据");
        }
    }

    /**
     * 锁定
     *
     * @param jsonData
     * @return
     */
    @AutoLog(value = "锁定")
    @PostMapping(value = "/lock")
    public Result<?> lock(@RequestBody JSONObject jsonData) {
        String id = jsonData.getString("id");
        Integer type = jsonData.getInteger("type");
        if (StringUtils.isNoneBlank(id) && null != type) {
            CompanyData companyData = companyDataService.getById(id);
            companyData.setIsLock(type);
            companyDataService.updateById(companyData);
            return Result.OK().success(type == 1 ? "已锁定!" : "已解锁");
        } else {
            return Result.error("请选择数据");
        }
    }

    /**
     * 添加
     *
     * @param companyData
     * @return
     */
    @AutoLog(value = "公司信息数据库-添加")
    @ApiOperation(value = "公司信息数据库-添加", notes = "公司信息数据库-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CompanyData companyData) {
        companyDataService.save(companyData);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param companyData
     * @return
     */
    @AutoLog(value = "公司信息数据库-编辑")
    @ApiOperation(value = "公司信息数据库-编辑", notes = "公司信息数据库-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CompanyData companyData) {
        companyDataService.updateById(companyData);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "公司信息数据库-通过id删除")
    @ApiOperation(value = "公司信息数据库-通过id删除", notes = "公司信息数据库-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        companyDataService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "公司信息数据库-批量删除")
    @ApiOperation(value = "公司信息数据库-批量删除", notes = "公司信息数据库-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.companyDataService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "公司信息数据库-通过id查询")
    @ApiOperation(value = "公司信息数据库-通过id查询", notes = "公司信息数据库-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CompanyData companyData = companyDataService.getById(id);
        if (companyData == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(companyData);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param companyData
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CompanyData companyData) {
        return super.exportXls(request, companyData, CompanyData.class, "公司信息数据库");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CompanyData.class);
    }

}
